package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.app.domain.AppSystemNotifications;
import com.study.app.domain.bo.AppSystemNotificationsBo;
import com.study.app.domain.vo.AppSystemNotificationsVo;
import com.study.app.mapper.AppSystemNotificationsMapper;
import com.study.app.service.IAppSystemNotificationsService;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.helper.AppLoginHelper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * App系统通知Service业务层处理
 *
 * @author magicyo
 * @date 2023-11-30
 */
@RequiredArgsConstructor
@Service
public class AppSystemNotificationsServiceImpl implements IAppSystemNotificationsService {

    private final AppSystemNotificationsMapper baseMapper;

    /**
     * 查询App系统通知
     */
    @Override
    public AppSystemNotificationsVo queryById(Long notificationId) {
        return baseMapper.selectVoById(notificationId);
    }

    /**
     * 查询App系统通知列表
     */
    @Override
    public TableDataInfo<AppSystemNotificationsVo> queryPageList(AppSystemNotificationsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppSystemNotifications> lqw = buildQueryWrapper(bo);
        Page<AppSystemNotificationsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<AppSystemNotificationsVo> queryPageListById(Long userId, PageQuery pageQuery) {
        LambdaQueryWrapper<AppSystemNotifications> lqw = Wrappers.lambdaQuery();
        lqw.eq(userId != null, AppSystemNotifications::getRecipientId, userId);
        lqw.orderByDesc(AppSystemNotifications::getSendTime);
        Page<AppSystemNotificationsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (AppSystemNotificationsVo record : result.getRecords()) {
            record.setMessageNum(this.getUserNoMessage(record.getRecipientId()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询App系统通知列表
     */
    @Override
    public List<AppSystemNotificationsVo> queryList(AppSystemNotificationsBo bo) {
        LambdaQueryWrapper<AppSystemNotifications> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppSystemNotifications> buildQueryWrapper(AppSystemNotificationsBo bo) {
        LambdaQueryWrapper<AppSystemNotifications> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRecipientId() != null, AppSystemNotifications::getRecipientId, bo.getRecipientId());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleCode()), AppSystemNotifications::getVehicleCode, bo.getVehicleCode());
        lqw.eq(StringUtils.isNotBlank(bo.getNotificationTitle()), AppSystemNotifications::getNotificationTitle, bo.getNotificationTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getNotificationContent()), AppSystemNotifications::getNotificationContent, bo.getNotificationContent());
        lqw.eq(bo.getSendTime() != null, AppSystemNotifications::getSendTime, bo.getSendTime());
        lqw.eq(bo.getIsRead() != null, AppSystemNotifications::getIsRead, bo.getIsRead());
        return lqw;
    }

    /**
     * 新增App系统通知
     */
    @Override
    public Boolean insertByBo(AppSystemNotificationsBo bo) {
        AppSystemNotifications add = BeanUtil.toBean(bo, AppSystemNotifications.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNotificationId(add.getNotificationId());
        }
        return flag;
    }

    /**
     * 修改App系统通知
     */
    @Override
    public Boolean updateByBo(AppSystemNotificationsBo bo) {
        AppSystemNotifications update = BeanUtil.toBean(bo, AppSystemNotifications.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppSystemNotifications entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除App系统通知
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Integer getUserNoMessage(Long userId) {
        return Math.toIntExact(baseMapper.selectCount(new LambdaQueryWrapper<AppSystemNotifications>()
                .eq(AppSystemNotifications::getIsRead, AppSystemNotifications.UNREAD)
                .eq(AppSystemNotifications::getRecipientId, userId)));
    }

    @Override
    public Boolean updateMessageRead(Long notificationId) {
        return baseMapper.update(null, new LambdaUpdateWrapper<AppSystemNotifications>()
                .eq(AppSystemNotifications::getNotificationId, notificationId)
                .set(AppSystemNotifications::getIsRead, AppSystemNotifications.READ)) > 0;
    }
    //查询车辆消息弹框提示
    @Override
    public AppSystemNotificationsVo queryVehicleSharingPopMessage() {
        AppSystemNotifications appSystemNotifications = new AppSystemNotifications();
        appSystemNotifications.setIsRead(AppSystemNotifications.UNREAD);
        appSystemNotifications.setIsType(AppSystemNotifications.POP);
        appSystemNotifications.setRecipientId(AppLoginHelper.getUserId());
        AppSystemNotificationsVo appSystemNotificationsVo = baseMapper.selectSysVehicleSharing(appSystemNotifications);
        return appSystemNotificationsVo;
    }

    @Transactional
    @Override
    public AppSystemNotificationsVo queryVehicleSharingTipMessage() {
        AppSystemNotifications appSystemNotifications = new AppSystemNotifications();
        appSystemNotifications.setIsRead(AppSystemNotifications.UNREAD);
        appSystemNotifications.setIsType(AppSystemNotifications.TIP);
        appSystemNotifications.setRecipientId(AppLoginHelper.getUserId());
        AppSystemNotificationsVo appSystemNotificationsVo = baseMapper.selectSysVehicleSharing(appSystemNotifications);
        //改变阅读状态
        this.updateMessageRead(appSystemNotificationsVo.getNotificationId());
        return appSystemNotificationsVo;
    }
}
